package com.loyalove.demo.akka;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class Hello {

  public static void main(String[] args) {
    ActorSystem actorSystem = ActorSystem.create("test");

    ActorRef helloRef = actorSystem.actorOf(Props.create(HelloActor.class), "hello");
    ActorRef hiRef = actorSystem.actorOf(Props.create(HiActor.class), "hi");

    hiRef.tell("Hello", helloRef);
  }

  private static class HelloActor extends AbstractActor {

    @Override
    public Receive createReceive() {

      return receiveBuilder()
          .match(Object.class, s -> {
            System.out.println(s);
            Thread.sleep(1000);
            getSender().tell("Hello", getSelf());
          }).build();
    }
  }

  private static class HiActor extends AbstractActor {

    @Override
    public Receive createReceive() {

      return receiveBuilder()
          .match(Object.class, s -> {
            System.out.println(s);
            Thread.sleep(1000);
            getSender().tell("Hi", getSelf());
          }).build();
    }
  }
}
